#include "nr_client_mainwindow.h"
#include "nr_client_dataAcquisition.h"
#include "nr_client_communication.h"


mainwindow::mainwindow(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
	,m_object_manager(nr_object_manager::instance())
{
	ui.setupUi(this);
//initialize rightclicked menu;
	m_last_mdiArea_Widget=0;
	m_Act_seeData = new QAction(tr("SeeData..."),this);
	m_Act_viewSerialServer = new QAction(tr("ViewSerialServer"),this);
	m_Rmenu_1 = new QMenu(this);
	m_Rmenu_1->addAction(m_Act_seeData);//creat viewdevice rightclicked menu;
	
	m_Rmenu_2 = new QMenu(this);
	m_Rmenu_2->addAction(m_Act_viewSerialServer);//creat viewserver RC menu;



	connect(m_Act_seeData,SIGNAL(triggered()),this,SLOT(on_SeeDeviceData()));
	connect(m_Act_viewSerialServer,SIGNAL(triggered()),this,SLOT(on_viewSerialServer()));
	connect(ui.device_alert_search,SIGNAL(triggered()),this,SLOT(on_device_alert_search_triggered()));
	

//initialize dockwindow;
// 	QDockWidget* dock = new QDockWidget(tr("Latest Alert Event"),this);
// 	dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
// 	QTextEdit* title = new QTextEdit();
// 	title->setText("Alert event:");
// 	dock->setWidget(title);
// 	dock->hide();
// 	this->addDockWidget(Qt::BottomDockWidgetArea,dock);

//  	QPalette palette;
//  	palette.setBrush(QPalette::Background, QBrush(QPixmap("systemhomepage.png")));
//  	this->setPalette(palette);

}

mainwindow::~mainwindow()
{
	delete m_Rmenu_1;
	delete m_treeWidget;
}


void mainwindow::initialize_tree( nr_device_item_server& device_item_server )
{
	m_treeWidget = new SetTreewidget( device_item_server );
	ui.scrollArea->setWidget(m_treeWidget);
	connect(m_treeWidget,SIGNAL(itemPressed(QTreeWidgetItem *, int)),
		this,SLOT(on_TreeWidget_Pressed(QTreeWidgetItem* ,int)));

	connect(m_treeWidget,SIGNAL( itemDoubleClicked(QTreeWidgetItem *, int ) ),
		this,SLOT(on_TreeItemDoubleClicked(QTreeWidgetItem*  ,int )) );
}

void mainwindow::set_CommunicationToServer( client_Communication& clientdeviceTree )
{
	m_clientdeviceTree = &clientdeviceTree;
	connect(m_clientdeviceTree, SIGNAL(client_received_deviceitem_fromserver(nr_device_item_server& )), 
		this, SLOT(initialize_tree(nr_device_item_server& )));
}


void mainwindow::on_TreeWidget_Pressed(QTreeWidgetItem *item, int column)
{
	if (qApp->mouseButtons() == Qt::LeftButton) //Left Mouse Button Clicked    
	{
		//on_TreeItemLeftClicked(item, column);
	}
	else if(qApp->mouseButtons() == Qt::RightButton) //Right Mouse Button Clicked
	{
		on_TreeItemRightClicked(item, column);
	}
}

void mainwindow::on_TreeItemRightClicked(QTreeWidgetItem *item, int column)
{
	QCursor cur=this->cursor(); 

	DeviceTreeWidgetItem *deviceitem = (DeviceTreeWidgetItem*) item;
	m_currentTreeWidgetItem = item;//the latest TreeWidgetItem what is Right Mouse Button Clicked.
	switch ( deviceitem->treenode_type() )
	{
//  	case DeviceTreeWidgetItem::treenode_type_t::nr_device_item_server:
// 		
// 		break;
	case DeviceTreeWidgetItem::nr_device_group:
		m_Rmenu_2->exec(cur.pos());
		break;
	case DeviceTreeWidgetItem::nr_device_item:
		m_Rmenu_1->exec(cur.pos());
		break;
	}
}

void mainwindow::on_SeeDeviceData()
{
	//QList<nr_device_id_t> device_id_list;
	//device_id_list.push_back(nr_device_id_t("APMD7XX000001"));
	//m_clientdeviceTree->start_GetdataFromServer(device_id_list);

	if ( m_last_mdiArea_Widget != 0 )
	{
		ui.mdiArea->removeSubWindow( m_last_mdiArea_Widget );
 		/*delete m_last_mdiArea_Widget;*/
 		m_last_mdiArea_Widget = 0;
	}
	if ( m_last_mdiArea_Widget==0 ) 
	{
		DeviceTreeWidgetItem *deviceitem = (DeviceTreeWidgetItem*)m_currentTreeWidgetItem;
		subwindow1 *sbw1 = new subwindow1;
/*		sbw1->set_client_dataAcquisition( this->get_last_mdiArea_Widget() );*/
		sbw1->set_device_TreeWidgetItem( *deviceitem );
		sbw1->set_DataCommunication( *m_clientdeviceTree );
		sbw1->set_device_type_manager( m_object_manager.devive_type_manager() );
		ui.mdiArea->addSubWindow(sbw1);
		sbw1->showMaximized();
		
		delete m_last_mdiArea_Widget;
		m_last_mdiArea_Widget = sbw1;

		// 	sbw1->show();
		// 	sbw1->setGeometry(ui.mdiArea->geometry());
	}
}

void mainwindow::on_viewSerialServer()
{
	if ( m_last_mdiArea_Widget != 0 )
	{
		ui.mdiArea->removeSubWindow( m_last_mdiArea_Widget );
		m_last_mdiArea_Widget = 0;
	}
	if ( m_last_mdiArea_Widget == 0 )
	{
		DeviceTreeWidgetItem *deviceitem = (DeviceTreeWidgetItem*)m_currentTreeWidgetItem;
		subwindow2 *sbw2 = new subwindow2;
		sbw2->set_device_TreeWidgetItem( *deviceitem );
		sbw2->set_DataCommunication( *m_clientdeviceTree );
		sbw2->set_device_type_manager( m_object_manager.devive_type_manager() );
		ui.mdiArea->addSubWindow(sbw2);
		sbw2->showMaximized();

		delete m_last_mdiArea_Widget;
		m_last_mdiArea_Widget = sbw2;
	}
}

void mainwindow::on_device_alert_search_triggered()
{
	QDockWidget* dock = new QDockWidget(tr("Latest Alert Event"),this);
	dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
	QTextEdit* title = new QTextEdit();
	title->setText("Alert event:");
	dock->setWidget(title);
	/*	dock->hide();*/
	this->addDockWidget(Qt::BottomDockWidgetArea,dock);
}

void mainwindow::on_TreeItemDoubleClicked( QTreeWidgetItem* item ,int column)
{
	QCursor cur=this->cursor(); 

	DeviceTreeWidgetItem *deviceitem = (DeviceTreeWidgetItem*) item;
	m_currentTreeWidgetItem = item;//the latest TreeWidgetItem what is Right Mouse Button Clicked.
	switch ( deviceitem->treenode_type() )
	{
	case DeviceTreeWidgetItem::nr_device_group:
		/*m_Rmenu_2->exec(cur.pos());*/
		break;
	case DeviceTreeWidgetItem::nr_device_item:
		this->on_SeeDeviceData();
		break;
	}
}